﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Module.Library;
using Csla.OpenExpressApp;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Types;
using OpenExpressApp.WPF.Command;
using OpenExpressApp.WPF;
using OpenExpressApp.Template;
using System.ComponentModel;
using Avalon.Windows.Controls;
using System.Windows.Controls;
using System.Windows;
using System.Diagnostics;

namespace OpenExpressApp.Module.WPF.Command
{
    [Command(CommandNames.SelectDataPermissionObject, TargetObjectType = typeof(OrgPositionDataPermission), Label = "选择对象", ToolbarType = ToolbarType.Child)]
    public class SelectDataPermissionObjectCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            return (null != view.Data);
        }

        public override void Execute(ListObjectView view)
        {
            var form = OpenApplication.CreatePopupForm(typeof(BusinessObjectInfo), "GetDataPermissionObjectList");
            bool? result = OpenApplication.ShowDialog(form, new DialogOptions()    //弹出弹出窗体
                {
                    AllowResize = true,
                    DialogTitle = "选择数据权限作用的对象",
                    DialogSize = new Size(800, 600)
                },
                null, new string[] { CommandNames.SelectAll }
            );

            if (result == true)
            {
                ObjectView selectView = (form as IFrameTemplate).View;
                OrgPositionDataPermissions items = view.Data as OrgPositionDataPermissions;
                foreach (BusinessObjectInfo src in selectView.GetActiveObjects())
                {
                    OrgPositionDataPermission item = (items as IBindingList).AddNew() as OrgPositionDataPermission;
                    item.OrgPositionId = (Guid)view.Parent.CurrentID;
                    item.OrgPosition = view.Parent.CurrentObject as OrgPosition;
                    item.BusinessObjectId = src.Id;
                    item.ModuleId = src.Module.Id;
                }
            }
        }
    }    

    [Command(CommandNames.EditDataPermissionExpr, ToolbarType = ToolbarType.Child, TargetObjectType = typeof(OrgPositionDataPermission), Label = "编辑可读表达式", ToolTip = "编辑可读表达式")]
    public class ExpressionEditorRatioCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            return (view.CurrentObject != null);
        }

        public override void Execute(ListObjectView view)
        {
            var form = new DataPermissionExpressionEditor(view);       
            string currentBoName = (view.CurrentObject as OrgPositionDataPermission).BoName;
            string commandName = CommandNames.SelectPBS;
            if (currentBoName == "项目信息")
                commandName = CommandNames.SelectProject;
            else if (currentBoName == "PBS模板")
                commandName = CommandNames.SelectPBSTemplate;
            else Debug.Assert(currentBoName == "PBS");
            
            bool? result = DialogWorkspace.ShowView(form, new DialogOptions()
            {
                DialogTitle = "编辑可读表达式",
                DialogSize = new Size(600, 400)
            }, null, new string[] {commandName});

            if (result == true)
            {
                (view.CurrentObject as OrgPositionDataPermission).Expression = form.Expression;
            }
        }
    }

    [Command(CommandNames.DataPermissionHelp, TargetObjectType = typeof(OrgPositionDataPermission), Label = "帮助", ToolbarType = ToolbarType.Child)]
    public class DataPermissionHelpCommand : WPFListViewCommand
    {
        public override void Execute(ListObjectView view)
        {
            TaskDialog taskDialog = new TaskDialog
            {
                Width = 600,
                Title = "表达式帮助",
                Content =  new TextBox()
                {
                    AcceptsReturn = true,
                    Text =
                    @"
【PBS】表达式举例说明
1. 显示整个项目和建安工程以及建安工程所有子
     InList(Name,""整个项目"",""建安工程"") or IsChildByFullName(""整个项目/建安工程"")
     注意:为了能构成树,有权限的节点必须能够成一颗树

【PBS模板】表达式举例说明
1. 显示一个PBS模板文件
    Code=""C1""
2.在多个PBS模板中，有选择性的显示2个或多个PBS模板文件,表达式为:
   （Code=""C1""） or （Code=""C2""） or （Code=""C3""）注意，“or”前后分别有空格

【项目信息】表达式举例说明
1. 根据项目PBS节点的属性值来过滤项目: 
    FloatPropertyValue(""建筑面积(m2)"")>10000    
    StringPropertyValue(""工程类型"")=""高层住宅""
   (FloatPropertyValue(""建筑面积(m2)"")>10000) and (StringPropertyValue(""工程类型"")=""高层住宅"")
2. 根据项目的编码/名称过滤项目: 
    Code=""GZ001S""    
    Name=""项目名称""
//",
//@"项目表达式: 
//1. 根据项目PBS的属性值来过滤项目: 
//   * FloatPropertyValue(""建筑面积(m2)"")>10000    
//   * StringPropertyValue(""工程类型"")=""高层住宅""
//   * (FloatPropertyValue(""建筑面积(m2)"")>10000) and (StringPropertyValue(""工程类型"")=""高层住宅"")
//2. 根据项目的编码/名称过滤: 
//   * Code=""GZ001Z""    
//   * Name=""项目名称""
//PBS
//1. 显示整个项目和建安工程以及建安工程所有子
//   * InList(Name,""整个项目"",""建安工程"") or IsChildByFullName(""整个项目/建安工程"")
//项目PBS: 
//1. 显示哪些PBS节点:  
//   InList(Name,""整个项目"",""建安工程"")  -- 注意:为了能构成树,有权限的节点必须能够成一颗树",
                }
            };

            taskDialog.Show();
        }
    }
}